package com.wd.tech.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wd.tech.R;
import com.wd.tech.adaptert.HomePageForDetailsAdaptert;
import com.wd.tech.base.App;
import com.wd.tech.base.BaseActivity;
import com.wd.tech.base.BasePreantert;
import com.wd.tech.bean.HomePageForDetailsBean;
import com.wd.tech.preantenrt.Preantert;
import com.wd.tech.utils.NetUtils;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class HomePageForDetailsActivity extends BaseActivity {
    @BindView(R.id.Home_rv)
    RecyclerView home_rv;
    @BindView(R.id.home_title)
    TextView home_title;
    @BindView(R.id.home_sou)
    TextView home_sou;
    @BindView(R.id.home_time)
    TextView home_time;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home_page_for_details;
    }

    @Override
    protected BasePreantert initPreantert() {
        return new Preantert(this);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        int id = intent.getIntExtra("home_id", 0);
        String title = intent.getStringExtra("title");
        String source = intent.getStringExtra("source");
        String format = intent.getStringExtra("format");
        home_title.setText(title);
        home_time.setText(format);
        home_sou.setText(source);
        Log.i("xxxx", id + "");
        SharedPreferences sp = App.getContext().getSharedPreferences("sp_id", MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt("id",id);
        edit.commit();
        NetUtils.getInstance().getApis().getHomePageForDetails(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HomePageForDetailsBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(HomePageForDetailsBean homePageForDetailsBean) {
                        HomePageForDetailsBean.ResultBean result = homePageForDetailsBean.getResult();
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplication());
                        home_rv.setLayoutManager(linearLayoutManager);
                        HomePageForDetailsAdaptert homePageForDetailsAdaptert = new HomePageForDetailsAdaptert(getApplicationContext(), result);
                        home_rv.setAdapter(homePageForDetailsAdaptert);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
